package com.lyfam.vaadin.service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.lyfam.vaadin.model.Deal;

public class FakeDealRepository implements DealRegistrationRepository
{

    private static Map<String, Deal> dealDB = new ConcurrentHashMap<String, Deal>();
    
    @Override
    public void deleteDeal(String dealId) throws Exception
    {
        // TODO Auto-generated method stub

    }

    @Override
    public Deal getDealById(String dealId) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @WebMethod
    public List<Deal> listDeals() throws Exception
    {
        return Arrays.asList(dealDB.values().toArray(new Deal[0]));
    }

    @Override
    @WebMethod
    public void saveDeal(@WebParam(name="deal") Deal deal) throws Exception
    {
        DwValidator val = new FakeValidator.ProjectNameValidator();
        
        if (!val.isValid(deal.getProjectName()))
        {
            throw new Exception("Value of project name is invalid");
        }
        
        dealDB.put(deal.getDealId(), deal);
    }

    public static Map<String, Deal> getDealDB()
    {
        return dealDB;
    }

    public static void setDealDB(Map<String, Deal> dealDB)
    {
        FakeDealRepository.dealDB = dealDB;
    }

}
